import gc
from Tkinter import *
from rendering import *
from vector import *
import sys
import os
images = []
textures = []
mapa = []
entity = []
class App:
    def __init__(self):
        gc.enable()
        self.running = True
        self.display = None
        self.frameCount = 0
        self.Start = False
        self.material = None
        #creates mapzor
        for x in range(0,64):
            mapa.append([])
            for y in range(0,48):
                mapa[x].append(("v"))              
    def init(self):
        pygame.init()
        self.display = pygame.display.set_mode((640,400), pygame.HWSURFACE)
        self.running = True
        self.w = Tk()
        self.createMenu()
        for buttonName in os.listdir("./textures/"):
            Button(master = self.w,text = buttonName, command = lambda name = buttonName:self.setMaterial(name)).pack()
    def createMenu(self):
        self.w1 = Tk()
        Button(master = self.w1,text="New",command = self.clean).pack(side = LEFT)
        Button(master = self.w1,text="Save",command = self.save).pack(side = LEFT)
        Button(master = self.w1,text="Exit",command = self.stop).pack(side = LEFT)
    def stop(self):
        quit()
    def clean(self):
        self.display.fill((0,0,0))
        for x in range(0,64):
            mapa.append([])
            for y in range(0,48):
                mapa[x].pop(y)
                mapa[x].append(("v"))
    def save(self):
        name = raw_input("Jmeno:")
        mapFile = open(str(name),"w")
        mapFile.write(str(textures)+";")
        mapFile.write(str(mapa))
    def setMaterial(self,material):
        self.material = material
        print "current material:" + self.material
    def event(self, event):
        if event.type == pygame.QUIT:
            self.running = False
            pygame.quit()
    def loop(self):
        pygame.display.flip()
    def cleanup(self):
        pygame.quit()
    def findIfIsOnList(self,text):
        for texture in textures:
            if texture == text:
                return 0
        textures.append(text)
    def execute(self):
        if self.init() == False:
            self.running = False 
        while(self.running):
            if pygame.mouse.get_pressed()[0] == 1 and self.material != None:
                pos = vector(int(pygame.mouse.get_pos()[0]/10)*10,int(pygame.mouse.get_pos()[1]/10)*10)
                print pos.get()
                mapa[int(pos.get()[0])/10].pop(int(pos.get()[1])/10)
                o = self.returnObject(self.material)
                mapa[int(pos.get()[0]/10)].insert(int(pos.get()[1]/10),(str(pos.get()[0]/10)+"x"+str(pos.get()[1]/10),self.material,True))
                self.findIfIsOnList(self.material)
                o.draw(pos)
            for event in pygame.event.get():
                self.event(event)
            self.loop()
            self.w.update()
            self.w1.update()
            if self.Start:
                pass
        self.cleanup()
    def returnObject(self,what):
        for index in range(0,len(images)):
            if images[index] == what:
                return index
                break
        images.append(i_object("./textures/"+self.material,self.display))
        return images[len(images)-1]
a = App()
a.execute()
